﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TotalFreedomKO
{
    public interface IKoElementHtmlString<TModel> : IKoAttributeHtmlString<TModel>
    {
        string ToHtmlString(IKoAttributeHtmlString<TModel> koAttribute);
    }

    public class KoElementHtmlString<TModel, TProperty> : KoAttributeHtmlString<TModel, TProperty>, IKoElementHtmlString<TModel>
    {
        private KoAttributeHtmlString<TModel, TProperty> _elementKoAttribute;

        private readonly string _startValue;
        private readonly string _endValue;
        private readonly string[] _supportedAttributes;

        public KoElementHtmlString(string startValue, string endValue, string attributeName, string attributeValue, ViewDataDictionary<TModel> viewData = null, string[] supportedAttributes = null)
            : base(null, attributeName, attributeValue, viewData, supportedAttributes)
        {
            _startValue = startValue;
            _endValue = endValue;
            _supportedAttributes = supportedAttributes;

            if (!String.IsNullOrEmpty(attributeName))
                _elementKoAttribute = new KoAttributeHtmlString<TModel, TProperty>(null, attributeName, attributeValue, viewData, supportedAttributes);
        }

        public override string ToHtmlString()
        {
            return _startValue + (_elementKoAttribute != null ? _elementKoAttribute.ToHtmlString() : "") + _endValue;
        }

        public string ToHtmlString(IKoAttributeHtmlString<TModel> koAttribute)
        {
            string d = "";
            string attributeValues = "";

            if (koAttribute != null)
            {
                List<KeyValuePair<string, string>> values = koAttribute.GetAttributeValues();
                if (values != null)
                {
                    foreach (KeyValuePair<string, string> value in values)
                    {
                        attributeValues += d + value.Value;
                        d = ", ";
                    }
                }
            }

            if(_elementKoAttribute != null)
                attributeValues = _elementKoAttribute._attributeValue + d + attributeValues;

            return _startValue + (!String.IsNullOrEmpty(attributeValues) ? " data-bind=\"" + attributeValues + "\"" : "") + _endValue;
        }

        public override bool IsKoAttributeSupported(string attributeName)
        {
            if (_elementKoAttribute != null && (!_elementKoAttribute.IsKoAttributeSupported(attributeName)))
                return false;
          
            if (_supportedAttributes != null && !_supportedAttributes.Contains(attributeName))
                    return false;

            return true;
        }

        public override bool HasKoElement()
        {
            return true;
        }

        public override IKoElementHtmlString<TModel> GetKoElement()
        {
            return this;
        }

        public override bool HasKoAttribute(string attributeName)
        {
            if (_elementKoAttribute != null)
                return _elementKoAttribute.HasKoAttribute(attributeName);

            return false;
        }
    }
}